package com.yjz.zpay.utils;

import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import de.robv.android.xposed.XposedBridge;


/**
 * Created by 35286 on 2019/3/25.
 */

public class Utils {

    private static final String TAG = "[^][ Utils ]";

    public static String getSubUtilSimple(String str, String str2) {
        Matcher matcher = Pattern.compile(str2).matcher(str);
        return matcher.find() ? matcher.group(1) : "";
    }

    public static String getNowDate() {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    }

    public static String getNowDate(long j) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
        Date date = new Date();
        date.setTime(j);
        return simpleDateFormat.format(date);
    }


    public static void xposedLog(String str) {
        XposedBridge.log(str);
        try {
            //获取日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//获取当前日期时间
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");//获取当前日期
            Date date = new Date(System.currentTimeMillis());//时间毫秒日期

            //创建日志的字符串
            StringBuilder stringBuilder = new StringBuilder(simpleDateFormat.format(date));
            stringBuilder.append(" ");
            stringBuilder.append(str);
            stringBuilder.append("\n");

            //创建文件的文件名字符串
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append(Environment.getExternalStorageDirectory().getPath());
            stringBuilder2.append("/WX_PAY_");
            stringBuilder2.append(simpleDateFormat2.format(date));
            stringBuilder2.append("_log.txt");

            //写成日志文件
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(stringBuilder2.toString())));
            bufferedWriter.write(stringBuilder.toString());
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (Exception e) {
            e.printStackTrace();

            //打印异常日志
            StringBuilder stringBuilder3 = new StringBuilder();
            stringBuilder3.append("[ Utils ] error 写日志文件异常, ");
            stringBuilder3.append(e.getMessage());
            XposedBridge.log(stringBuilder3.toString());
        }
    }

    public static void textLog(String str) {
        Log.i(TAG, str);
        /*try {
            //获取日期
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//获取当前日期时间
            SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");//获取当前日期
            Date date = new Date(System.currentTimeMillis());//时间毫秒日期

            //创建日志的字符串
            StringBuilder stringBuilder = new StringBuilder(simpleDateFormat.format(date));
            stringBuilder.append(" ");
            stringBuilder.append(str);
            stringBuilder.append("\n");

            //创建文件的文件名字符串
            StringBuilder stringBuilder2 = new StringBuilder();
            stringBuilder2.append(Environment.getExternalStorageDirectory().getPath());
            stringBuilder2.append("/WX_PAY_");
            stringBuilder2.append(simpleDateFormat2.format(date));
            stringBuilder2.append("_log.txt");

            //写成日志文件
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(new File(stringBuilder2.toString())));
            bufferedWriter.write(stringBuilder.toString());
            bufferedWriter.flush();
            bufferedWriter.close();

        } catch (Exception e) {
            e.printStackTrace();

            //打印异常日志
            StringBuilder stringBuilder3 = new StringBuilder();
            stringBuilder3.append("[ Utils ] error 写日志文件异常, ");
            stringBuilder3.append(e.getMessage());
            Log.e(TAG, stringBuilder3.toString());
        }*/
    }


}
